package com.mynews.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.mynews.R;
import com.mynews.adapter.HomeFragmentRecyAdapter;
import com.mynews.bean.DailyListBean;
import com.mynews.mvp.presenter.LatestDailyPresenter;
import com.mynews.mvp.view.LatestDailyViewInterface;
import com.mynews.test.HeaderAndFooterWrapper;
import com.mynews.test.MyRefreshRecyclerView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/6/23.
 */

public class HomeFragment extends Fragment implements LatestDailyViewInterface {

    private static final String TAG = HomeFragment.class.getSimpleName();
    @Bind(R.id.home_fragment_recycleview)
    MyRefreshRecyclerView homeFragmentRecycleview;
    @Bind(R.id.net_fail_img)
    ImageView netFailImg;

    private LatestDailyPresenter presenter;
    private HomeFragmentRecyAdapter homeFragmentRecyAdapter;
    private String netDate;
    private boolean isLoading = true;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View contentView = inflater.inflate(R.layout.fragment_home, null);
        ButterKnife.bind(this, contentView);
        init();
        return contentView;
    }

    private HeaderAndFooterWrapper headerAndFooterWrapper;

    private void init() {
        presenter = new LatestDailyPresenter(this);
        presenter.doLoadData();

        final LinearLayoutManager manager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        homeFragmentRecycleview.setLayoutManager(manager);
        homeFragmentRecyAdapter = new HomeFragmentRecyAdapter(getActivity());

        headerAndFooterWrapper = new HeaderAndFooterWrapper(homeFragmentRecyAdapter,getContext());

        View headRefreshView = homeFragmentRecycleview.getHeadRefreshView();
        headerAndFooterWrapper.addHeaderView(headRefreshView);
//        headerAndFooterWrapper.addHeaderView(textView);
        homeFragmentRecycleview.setAdapter(headerAndFooterWrapper);

        homeFragmentRecycleview.setRefreshClick(new MyRefreshRecyclerView.RefreshClick() {
            @Override
            public void refreshing() {
                presenter.doLoadData();
            }
        });



        homeFragmentRecycleview.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                Log.e(TAG, "onScrolled: "+dy );
                super.onScrolled(recyclerView, dx, dy);
                int childCount = recyclerView.getChildCount();
                int itemCount = manager.getItemCount();
                int lastVisibleItemPosition = manager.findLastCompletelyVisibleItemPosition();
                int firstVisibleItemPosition = manager.findFirstCompletelyVisibleItemPosition();
                Log.e(TAG, "onScrolled: childCount："+childCount+"|firstpoi:"+firstVisibleItemPosition
                +"|lastpoi:"+lastVisibleItemPosition+"|itemCount:"+itemCount);
                if(lastVisibleItemPosition > itemCount -2 && !isLoading){
                    presenter.doLoadBeforeData(netDate);
                    isLoading = true;
                }
                if(firstVisibleItemPosition == 0){
                    homeFragmentRecyAdapter.setDx(dy);
                    Log.e(TAG, "onScrolled: "+dy );
                }
            }
        });

    }

//    private void initRecyRefreshView() {
//        //
//        View headFlushView = homeFragmentRecycleview.getChildAt(0);
//        Log.e(TAG, "initRecyRefreshView: "+ headFlushView);
//        if(headFlushView != null){
//            View viewRootLl = headFlushView.findViewById(R.id.root_ll);
//            ViewGroup.LayoutParams layoutParams = viewRootLl.getLayoutParams();
//            Log.e(TAG, "initRecyRefreshView: "+ layoutParams.height);
//            layoutParams.height = 0;
//            viewRootLl.setLayoutParams(layoutParams);
//        }
//    }

    public static Fragment newInstance() {
        Fragment fragment = new HomeFragment();
        return fragment;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    private void showNetFail(boolean isShow){
        if(isShow){
            netFailImg.setVisibility(View.VISIBLE);
        }else {
            netFailImg.setVisibility(View.INVISIBLE);
        }
    }


    /****************************
     * 网络数据加载回调
     ******************************************/
    @Override
    public void LatestDailyDisView(DailyListBean dailyListBean) {
        isLoading = false;
        Log.e(TAG, "LatestDailyDisView: " + dailyListBean.toString());
        showNetFail(false);
        homeFragmentRecyAdapter.addData(dailyListBean);
        netDate = dailyListBean.getDate();
        headerAndFooterWrapper.notifyDataSetChanged();

        homeFragmentRecycleview.refreshFinish(true);
    }

    @Override
    public void LatestDilyError() {
        isLoading = false;
        showNetFail(true);
        homeFragmentRecycleview.refreshFinish(false);

    }

    @Override
    public void BeforeDailyShowContent(DailyListBean dailyBeforeListBean) {
        isLoading = false;
        netDate = dailyBeforeListBean.getDate();
        List<DailyListBean.StoriesBean> stories = dailyBeforeListBean.getStories();
        DailyListBean.StoriesBean storiesBean = new DailyListBean.StoriesBean();
        storiesBean.setContentType(DailyListBean.CONTENT_DATE_TYPE);
        storiesBean.setDate(dailyBeforeListBean.getDate());
        stories.add(0,storiesBean);

        dailyBeforeListBean.setStories(stories);

        homeFragmentRecyAdapter.addData(dailyBeforeListBean);
        headerAndFooterWrapper.notifyDataSetChanged();

//        initRecyRefreshView();

    }

    @Override
    public void BeforeDailyError() {
        isLoading = false;

    }
}
